<?php
class editnewsAction extends sfActions
{
	public function execute()
	{
		$this->id = $this->getRequestParameter('id');
		if ($this->getRequest()->getMethod() == sfRequest::POST)
		{
			$idolNews = $this->getRequestParameter('idolnews');
			// For delete
			if($idolNews['act'] =='delete')
			{
				$this->idol_news = IdolNewsPeer::retrieveByPk($idolNews['id']);
				if(!$this->deleteIdolNews($this->idol_news)){

					return $this->redirect("/idol/news");
				}
			}
			// For update and add new
			$this->idol_news = $this->getIdolNewsCreate($idolNews['id']);
			$this->updateIdolNewsFromRequest();
			$this->saveIdolNews($this->idol_news);
			if($idolNews['save_and_add'])
			{
				return $this->forward('idol', 'createNews');
			}
			return $this->forward('idol', 'news');
		}
		// for view edit
		$news_arr = array();
		if($this->id)
		{
			$this->getLabels();
			$c = new Criteria();
			$c->add(IdolNewsPeer::ID,$this->id);
			$news = IdolNewsPeer::doSelectOne($c);
			$news_arr = $news->toArray();
		}
		$this->news_item = $news_arr;
	}

	protected function deleteIdolNews($IdolNews)
	{
		return $IdolNews->delete();
	}
	protected function saveIdolNews($idol_news)
	{
		$idol_news->save();

	}
	protected function updateIdolNewsFromRequest()
	{
		$idol_news = $this->getRequestParameter('idolnews');
		if (isset($idol_news['title']))
		{
			$this->idol_news->setTitle($idol_news['title']);
		}
		if (isset($idol_news['quote']))
		{
			$this->idol_news->setQuote($idol_news['quote']);
		}
		if (isset($idol_news['content']))
		{
			$this->idol_news->setContent($idol_news['content']);
		}
		if (isset($idol_news['enable']))
		{
			$this->idol_news->setEnable($idol_news['enable']);
		}
		else
		{
			$this->idol_news->setEnable(0);
		}
		if (isset($idol_news['contest_rule']))
		{
			$this->idol_news->setContestRules($idol_news['contest_rule']);
		}
		else
		{
			$this->idol_news->setContestRules(0);
		}
		if (isset($idol_news['create_datetime']))
		{
			if ($idol_news['create_datetime'])
			{
				try
				{
					$dateFormat = new sfDateFormat($this->getUser()->getCulture());
					if (!is_array($idol_news['create_datetime']))
					{
						$value = $dateFormat->format($idol_news['create_datetime'], 'I', $dateFormat->getInputPattern('g'));
					}
					else
					{
						$value_array = $idol_news['create_datetime'];
						$value = $value_array['year'].'-'.$value_array['month'].'-'.$value_array['day'].(isset($value_array['hour']) ? ' '.$value_array['hour'].':'.$value_array['minute'].(isset($value_array['second']) ? ':'.$value_array['second'] : '') : '');
					}
					$this->idol_news->setCreateDatetime($value);
				}
				catch (sfException $e)
				{
					// not a date
				}
			}
			else
			{
				$this->faq_items->setCreateDatetime(null);
			}
		}
	}

	protected function getIdolNewsCreate($id)
	{
		if (!$id)
		{
			$idol_news = new IdolNews();
		}
		else
		{
			$idol_news = IdolNewsPeer::retrieveByPk($id	);
		}

		return $idol_news;
	}

	protected function getIdolNewsOrCreate($id = 'id')
	{
		if (!$this->getRequestParameter($id))
		{
			$idolNews = new IdolNews();
		}
		else
		{
			$idolNews = IdolNewsPeer::retrieveByPk($this->getRequestParameter($id));
			$this->forward404Unless($idolNews);
		}

		return $idolNews;
	}
	protected function getLabels()
	{
		return array(
      'idol_news{title}' => 'Title:',
      'idol_news{sort}' => 'Sort:',
      'idol_news{enable}' => 'Enable:',
      'idol_news{contest_rules}' => 'Show default:',
      'idol_news{content}' => 'Content:',
      'idol_news{create_datetime}' => 'Create datetime:',
		);
	}
}
?>